package u1project.client;

import u1project.common.Request;
import u1project.common.Response;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class MyClient {
    private static Socket s;
    private static ObjectOutputStream oout;
    private static ObjectInputStream oin;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        try {
            Properties prop = new Properties();
            prop.load(MyClient.class.getClassLoader().getResourceAsStream("u1project/client/ip.properties"));
            String ip = prop.getProperty("ServerIp", "localhost");
            int port = Integer.parseInt(prop.getProperty("port", "9999"));
            System.out.println(ip + "," + port);
            //Socket s=new Socket("192.168.1.135",9996);
            s = new Socket(ip, port);
            oout = new ObjectOutputStream(s.getOutputStream());
            oin = new ObjectInputStream(s.getInputStream());
            while (true) {
                System.out.println("1:登录，2：注册 3：上传 9：退出");
                int choice = in.nextInt();
                if (choice == 1) {
                    System.out.println("请输入用户名:");
                    String n = in.next();
                    System.out.println("请输入用户密码:");
                    String p = in.next();
                    Request request = new Request("login");
                    request.put("name", n);
                    request.put("password", p);
//                    oout.writeObject(request);
//                    oout.reset();
//                    Response resp = (Response) oin.readObject();
                    Response resp = send(request);
                    if (resp.isSucess()) {
                        System.out.println("成功成功");
                        showNovelType();
                    } else {
                        System.out.println("失败了，原因是" + resp.getMsg());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void showNovelType() throws Exception {
        Scanner in = new Scanner(System.in);
        int choice = 0;
        while (true) {
            System.out.println("0:返回上一级\n1： 武侠\n2：言情");
            choice = in.nextInt();
            if (choice == 0) {
                return;
            } else if (choice == 1) {
                Request re = new Request("findNovel");
                re.put("noveltype", "武侠");
                Response resp = send(re);
                if (resp.isSucess()) {
                    List<String> novels = (List<String>) resp.get("novels");
                    showNovels("武侠", novels);
//
//                    for(int i=0;i<novels.size();i++){
//                        System.out.println((i+1)+" "+novels.get(i));
//                    }
//                    System.out.println("阅读和下载请选择序号，上传txt请输入-1，返回请输入0");
                } else {
                    System.out.println("错误：" + resp.getMsg());
                }
            } else if (choice == 2) {
                Request re = new Request("findNovel");
                re.put("noveltype", "言情");
                Response resp = send(re);
                if (resp.isSucess()) {
                    List<String> novels = (List<String>) resp.get("novels");
                    for (int i = 0; i < novels.size(); i++) {
                        System.out.println((i + 1) + " " + novels.get(i));
                    }
                    System.out.println("阅读和下载请选择序号，上传txt请输入-1，返回请输入0");
                } else {
                    System.out.println("错误：" + resp.getMsg());
                }
            } else {
                System.out.println("输入错误");
            }
        }
    }

    private static void showNovels(String type, List<String> novels) {
        for (int i = 0; i < novels.size(); i++) {
            System.out.println((i + 1) + " " + novels.get(i));
        }
        Scanner in = new Scanner(System.in);
        int choice = 0;

        while (true){
            System.out.println("阅读和下载请选择序号，上传txt请输入-1，返回请输入0");
            choice=in.nextInt();
            if(choice==0)
                return;
            else if(choice==-1){

            }
            choice--;
            if(!(choice>=0&&choice<novels.size())){
                System.out.println("输入错误");
                continue;
            }
            String novelName=novels.get(choice);

        }

    }

    public static Response send(Request re) throws Exception {
        oout.writeObject(re);
        oout.reset();
        Response resp = (Response) oin.readObject();
        return resp;
    }

//    public static void selNovelType() {
//
//    }
}
